package com.zide.platform.mapper;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zide.common.config.mybatisplus.ZiDeMapperPlus;
import com.zide.common.core.domain.BaseEntity;
import com.zide.common.core.domain.PageResult;
import com.zide.platform.entity.PlatformPost;
import com.zide.platform.vo.search.PlatformPostSearchVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 参数配置 数据层
 *
 * @author zide
 */
@Mapper
public interface PlatformPostMapper extends ZiDeMapperPlus<PlatformPost> {

    default PageResult<PlatformPost> selectPage(PlatformPostSearchVO searchVO) {
        LambdaQueryWrapper<PlatformPost> wrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotEmpty(searchVO.getPostCode())) {
            wrapper.like(PlatformPost::getPostCode,searchVO.getPostCode());
        }
        if(StrUtil.isNotEmpty(searchVO.getPostName())) {
            wrapper.like(PlatformPost::getPostName,searchVO.getPostName());
        }
        if(ObjectUtil.isNotEmpty(searchVO.getStatus())) {
            wrapper.eq(PlatformPost::getStatus,searchVO.getStatus());
        }
        wrapper.orderByDesc(BaseEntity::getCreateTime);
        return selectPage(searchVO, wrapper);
    }

    @Select("select p.post_id\n" +
            "        from platform_post p\n" +
            "\t        left join platform_user_post up on up.post_id = p.post_id\n" +
            "\t        left join platform_user u on u.id = up.user_id\n" +
            "\t    where u.id = #{userId}")
    List<Long> selectPostListByUserId(@Param("userId") Long userId);

}
